home *** CD-ROM | disk | FTP | other *** search
- //----------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------
- //
- // Filename : top10.cpp
- // Description : Member Definitions for top10 class
- // Author : Marnich van Rensburg (2002)
- //
- //----------------------------------------------------------------------------------------
- //----------------------------------------------------------------------------------------
-
- #include "top10.h"
-
-
- //----------------------------------------------------------------------------------------
- // Description : Constructor
- //----------------------------------------------------------------------------------------
-
- Top10 :: Top10() // Initialize Our Timer (Get It Ready)
- {
- for(int n = 0;n < 10; n++)
- {
- for(int c = 0; c < 23; c++)
- List[n].Name[c] = '.';
- List[n].Name[23] = '\0';
- List[n].Level = 0;
- List[n].Lines = 0;
- List[n].Score = 0;
- }//for
-
- } // Top10
-
-
- //----------------------------------------------------------------------------------------
- // Description : Init highscore table
- //----------------------------------------------------------------------------------------
-
- void Top10 :: Init()
- {
- // Load Scores from file
- if(!Load())
- {
- // If theres no file asume defaults
- for(int n = 0;n < 10; n++)
- {
- for(int c = 0; c < 23; c++)
- List[n].Name[c] = '.';
- List[n].Name[23] = '\0';
- List[n].Level = 0;
- List[n].Lines = 0;
- List[n].Score = 0;
- }//for
- Save();
- }//if
-
- }//end Init
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Save scores to file
- //----------------------------------------------------------------------------------------
-
- void Top10 :: Save()
- {
- FILE *p_File;
-
- p_File = fopen("data/scores.dat", "w");
-
- for(int n = 0;n < 10; n++)
- {
- fprintf(p_File, "%s", List[n].Name);
- fprintf(p_File, "%c", '\0');
- fprintf(p_File, "%c", ',');
- fprintf(p_File, "%u", List[n].Score);
- fprintf(p_File, "%c", ',');
- fprintf(p_File, "%u", List[n].Lines);
- fprintf(p_File, "%c", ',');
- fprintf(p_File, "%u", List[n].Level);
- fprintf(p_File, "%c", ',');
- }//for
-
- if(p_File)
- {
- fclose(p_File);
- Encrypt("data/scores.dat"); //encrypt the file
- }//if
-
- }//end Save
-
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Load scores from file
- //----------------------------------------------------------------------------------------
-
- bool Top10 :: Load()
- {
- Encrypt("data/scores.dat"); //decrypt the file
- FILE *p_File = NULL;
-
- p_File = fopen("data/scores.dat", "r");
-
- if(p_File)
- {
- for(int n = 0;n < 10; n++)
- fscanf(p_File, "%24s,%u,%u,%u,", &List[n].Name, &List[n].Score, &List[n].Lines, &List[n].Level);
-
- fclose(p_File);
-
- Encrypt("data/scores.dat"); //encrypt the file
- return true;
- }else{
- Encrypt("data/scores.dat"); //encrypt the file
- return false;
- }//if
-
- }//end Load
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Adds a new entry onto the top10 list
- //----------------------------------------------------------------------------------------
-
- void Top10 :: SetEntry(char v_Name[25], unsigned int v_Score, unsigned int v_Lines, unsigned int v_Level)
- {
- int n = 0;
-
- //Determine where the entry should go (ep = new entry position)
- for(int ep = 0;(ep < 10) && (v_Score < List[ep].Score); ep++)
- ;
-
- // if new intry not at last positon on list
- if(ep != 9)
- {
- //shift entries down from the new entry's position
- for(n = 9; n > ep; n--)
- Copy(List[n - 1], List[n]);
- }//if
-
- //Insert new entry
- for(n = 0; n < 23; n++)
- List[ep].Name[n] = v_Name[n];
-
- List[ep].Score = v_Score;
- List[ep].Level = v_Level;
- List[ep].Lines = v_Lines;
-
- // Save the updated highscore table
- Save();
-
- }//end SetEntry
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Used for checking if the player has a new highscore
- //----------------------------------------------------------------------------------------
-
- bool Top10 :: IsNewHighScore(unsigned int v_Score)
- {
- if(v_Score > List[9].Score)
- return true;
- else
- return false;
-
- }//end IsNewHighScore
-
-
- //----------------------------------------------------------------------------------------
- // Description : Makes a copy of a highscore entry
- //----------------------------------------------------------------------------------------
-
- void Top10 :: Copy(HighScore& ref_Source, HighScore& ref_Dest)
- {
-
- for(int n = 0; n < 23; n++)
- ref_Dest.Name[n] = ref_Source.Name[n];
-
- ref_Dest.Score = ref_Source.Score;
- ref_Dest.Lines = ref_Source.Lines;
- ref_Dest.Level = ref_Source.Level;
-
- }//end Copy
-
-
-
- //----------------------------------------------------------------------------------------
- // Description : Very elementary encryption for encrypting highscore file
- // to decrypt just pass encrypted file through the function again
- //----------------------------------------------------------------------------------------
-
- void Top10 :: Encrypt(const char *TargetFile)
- {
- FILE *p_if = NULL;
- FILE *p_of = NULL;
- int c;
-
- p_if = fopen(TargetFile, "r");
- p_of = fopen("data/tmp", "w");
-
- if(p_if && p_of)
- {
- while ((c = fgetc(p_if)) != EOF)
- fputc((unsigned char)c + 128, p_of);
-
- fclose(p_if);
- fclose(p_of);
-
- remove(TargetFile);
- rename("data/tmp", TargetFile);
- }//if
-
- }//end Encrypt